﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.Text;

namespace HTTPDebuggerTools.com
{
    public partial class URLEncodeDecode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NoBotState state;
            if (!NoBot1.IsValid(out state))
                Response.Redirect("http://www.httpdebugger.com/");
        }

        protected void EncodeBtn_Click(object sender, EventArgs e)
        {
            ResultBox.Visible = false;
            ErrorLabel.Visible = false;

            string src = SourceBox.Text.Trim();
            if (src.Length != 0)
            {
                try
                {
                    ResultBox.Text = Server.UrlEncode(src);
                    ResultBox.Visible = true;
                }
                catch
                {
                    ErrorLabel.Text = "Error occurred while encoding the URL.";
                    ErrorLabel.Visible = true;
                }
            }
        }

        protected void DecodeBtn_Click(object sender, EventArgs e)
        {
            ResultBox.Visible = false;
            ErrorLabel.Visible = false;

            string src = SourceBox.Text.Trim();
            if (src.Length != 0)
            {
                try
                {
                    ResultBox.Text = Server.UrlDecode(src);
                    ResultBox.Visible = true;
                }
                catch
                {
                    ErrorLabel.Text = "Error occurred while decoding the URL.";
                    ErrorLabel.Visible = true;
                }
            }
        }

        protected void EscapeBtn_Click(object sender, EventArgs e)
        {
            ResultBox.Visible = false;
            ErrorLabel.Visible = false;

            string src = SourceBox.Text.Trim();
            if (src.Length != 0)
            {
                try
                {
                    ResultBox.Text = Uri.EscapeUriString(src);
                    ResultBox.Visible = true;
                }
                catch
                {
                    ErrorLabel.Text = "Error occurred while escaping the URL.";
                    ErrorLabel.Visible = true;
                }
            }
        }

        protected void UnescapeBtn_Click(object sender, EventArgs e)
        {
            ResultBox.Visible = false;
            ErrorLabel.Visible = false;

            string src = SourceBox.Text.Trim();
            if (src.Length != 0)
            {
                try
                {
                    ResultBox.Text = Uri.UnescapeDataString(src);
                    ResultBox.Visible = true;
                }
                catch
                {
                    ErrorLabel.Text = "Error occurred while unescaping the URL.";
                    ErrorLabel.Visible = true;
                }
            }
        }
    }
}
